package common.components.smileyenhanced
{
	import mx.controls.TextInput;
	import flash.events.Event;
	import flash.events.TextEvent;
	import mx.controls.Image;
	import flash.geom.Rectangle;
	import mx.core.UIComponent;
	import flash.display.Sprite;
	import mx.core.BitmapAsset;
	import mx.binding.utils.ChangeWatcher;
	import flash.text.TextField;

	public class SmileyTextInput extends TextInput implements ISmileyTextEditable
	{
		[Bindable]
		[Embed(source="smilies/smile.gif")]
		private var smileIconClass:Class;
		[Bindable]
		[Embed(source="smilies/sad.gif")]
		private var sadIconClass:Class;
		[Bindable]
		[Embed(source="smilies/veryhappy.gif")]
		private var happyIconClass:Class;
		[Bindable]
		[Embed(source="smilies/wink.gif")]
		private var winkIconClass:Class;
		[Bindable]
		[Embed(source="smilies/shocked.gif")]
		private var shockedIconClass:Class;
		[Bindable]
		[Embed(source="smilies/crying.gif")]
		private var cryingIconClass:Class;
		[Bindable]
		[Embed(source="smilies/confused.gif")]
		private var confusedIconClass:Class;
		[Bindable]
		[Embed(source="smilies/bored.gif")]
		private var boredIconClass:Class;
		[Bindable]
		[Embed(source="smilies/tongue.gif")]
		private var tongueIconClass:Class;
		
		public var smilies:Array = [
		    				  [[":)",":-)"],smileIconClass],
		    				  [[":(",":-("],sadIconClass],
		    				  [[":D",":-D"],happyIconClass],
		    				  [[";)",";-)"],winkIconClass],
		    				  [[":O",":o",":-o",":-O",":0",":-0"],shockedIconClass],
		    				  [[";(",";~(",";-("],cryingIconClass],
		    				  [[":s",":-s"],confusedIconClass],
		    				  [[":|",":-|"],boredIconClass],
		    				  [[":P",":p",":-p",":-P"],tongueIconClass]
		    				];
		private var smilyImages:Array = [];
		public var verticalScrollPosition:int = 0;
		public function SmileyTextInput()
		{
			super();
			this.addEventListener("textInput", handleTextInput);
		}
		override public function get text ():String {
			return this.realText;
		}
		public function get realText ():String {
			var ind:int = 0;
			var res:String = "";
			this.smilyImages.sortOn("position", Array.NUMERIC);
			for(var i:int=0; i<this.smilyImages.length; i++){
				var curSm:Smiley = Smiley(this.smilyImages[i]);
				res += this.textField.text.substring(ind, curSm.position);
				res += curSm.text;
				ind = curSm.position+curSm.replaceLength;
			}
			res += this.textField.text.substring(ind);
			return res;
		}
		public function set realText (str:String):void {
			for(var i:int=0; i<this.smilyImages.length; i++){
				this.removeSmily(Smiley(this.smilyImages[i]));
			}
			this.textField.text = str;
			this.dispatchEvent(new TextEvent("textInput", false, false, str));
		}
		private function handleTextInput (evt:TextEvent):void{
			var buf:String = "";
			var t:TextField = this.textField;
			for(var i:int=0; i<t.text.length; i++) {
				buf += t.text.charAt(i);
				var foundStart:Boolean = false;
				var found:Boolean = false;
				for(var j:int=0; j<this.smilies.length; j++){
					var cur:Array = this.smilies[j][0];
					for(var k:int=0; k<cur.length; k++){
						if(cur[k].substr(0,buf.length)==buf){
							foundStart = true;
							if(cur[k].length == buf.length){
								found = true;
								break;
							}
						}
					}
					if(found) break;
				}
				if(found){
					var newSmily:Smiley = new Smiley(smilies[j][1], t, this, i-buf.length+1, buf);
					i += newSmily.replaceLength - buf.length;
					buf = "";
					this.smilyImages.push(newSmily);
				}
				if(!foundStart) buf = "";
				if(buf.length==3) buf = buf.substr(1,2);	
			}
		}
		public function removeSmily (sm:Smiley):void {
			if(sm.parent==this){
				this.removeChild(sm);
			}
			for(var i:int = 0; i<this.smilyImages.length; i++){
				if(this.smilyImages[i] == sm){
					this.smilyImages.splice(i, 1);
					break;
				}
			}
		}
	}
}